const myDb = require('../../../libs/mydb')

const baseModel = require('../../common/models/BaseModel')

const myCrypto = require('../../../utiles/mycrypto')

module.exports = {
    /**
     * 
     * @param {*} req 
     * @param {*} res 
     */
    // 后台账号验证
    check_user: async function (req, res) {
        let { uname, upassword } = req.body;

        if (baseModel.check_empty(uname)) {
            res.send({
                code: 400,
                msg: 'Data is not empty'
            })
        } else {
            await myDb.singleSearch('ac_admin', 'a_password,a_id,a_role_id', ` WHERE a_name='${uname}'`)
                .then(
                    result => {
                        if (result.length == 0) {
                            res.send({
                                code: 401,
                                msg: 'uname is no find'

                            })
                        } else {

                            if (result[0].a_password != myCrypto.md5(upassword)) {
                                res.send({
                                    code: 402,
                                    msg: 'upassword is no find'

                                })

                            } else {

                                req.session.user = {
                                    aid: result[0].a_id,
                                    role: result[0].a_role_id,
                                    adname: uname
                                }
                                res.send({
                                    code: 200,
                                    msg: 'ok'
                                    
                                })
                                
                            }

                        }
                    }
                )

        }


    }

    
}